package com.chcplayer.ui
{
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

/**
 * author yh
 */
public class FourSidedTextInput extends Sprite
{
	/**
	 */
	public static const TEXT_FORMAT:TextFormat = new TextFormat("Microsoft YaHei", 20);
	/**
	 */
	public static const TEXT_WIDTH:int = 100;
	/**
	 */
	public static const TEXT_HEIGHT:int = 30;
//--------------------------------------------------------------------------------------------------
// private
//--------------------------------------------------------------------------------------------------
	/**
	 */
	private var m_ttfTop:TextField = new TextField();
	private var m_ttfBottom:TextField = new TextField();
	/**
	 */
	private var m_ttfLeft:TextField = new TextField();
	private var m_ttfRight:TextField = new TextField();
	/**
	 */
	public function FourSidedTextInput()
	{
		super();
		this._initText(m_ttfTop);
		this._initText(m_ttfBottom);
		this._initText(m_ttfLeft);
		this._initText(m_ttfRight);
		m_ttfTop.x = m_ttfBottom.x = TEXT_WIDTH >> 1;
		m_ttfLeft.y = m_ttfRight.y = TEXT_HEIGHT + 1;
		m_ttfRight.x = TEXT_WIDTH + 1;
		m_ttfBottom.y = TEXT_HEIGHT * 2 + 2;
		/*
		*/
		this.addChild(m_ttfTop);
		this.addChild(m_ttfBottom);
		/*
		*/
		this.addChild(m_ttfLeft);
		this.addChild(m_ttfRight);
	}
//--------------------------------------------------------------------------------------------------
// private
//--------------------------------------------------------------------------------------------------
	private function _initText(ttf:TextField):void
	{
		TEXT_FORMAT.align = TextFormatAlign.CENTER;
		ttf.defaultTextFormat = TEXT_FORMAT;
		ttf.width = TEXT_WIDTH;
		ttf.height = TEXT_HEIGHT;
		ttf.restrict = "0-9";
		ttf.background = true;
		ttf.type = TextFieldType.INPUT;
	}
//==================================================================================================
// public
//==================================================================================================
	public function setTextFromRect(rect:Rectangle):void
	{
		if ( rect != null )
		{
			m_ttfTop.text = String(rect.y);
			m_ttfLeft.text = String(rect.x);
			m_ttfRight.text = String(rect.width);
			m_ttfBottom.text = String(rect.height);
		}
	}
	/**
	 */
	public function getRectFromText():Rectangle
	{
		var rect:Rectangle = new Rectangle();
		rect.x = int(m_ttfLeft.text);
		rect.y = int(m_ttfTop.text);
		rect.width = int(m_ttfRight.text);
		rect.height = int(m_ttfBottom.text);
		return rect;
	}
}
}